<template>
  <div>
    <div class="app-container clearfix pageHeader">
      <div class="header fl" style="padding: 22px 0;">{{$route.meta.title}}</div>
      <div class="extra fr">
        <el-button @click="back">返回</el-button>
      </div>
    </div>
    <el-card shadow="never" class="app-container margin-content no-padding">
      <div slot="header" class="clearfix">
        <div class="header fl">吊装费用明细（包括吊运、托运费用）（单位：元）</div>
        <div class="extra fr">
          <el-alert
            :title="currentAuditStatus.text"
            :type="currentAuditStatus.type"
            show-icon
            :closable="false"
            v-if="true">
          </el-alert>
        </div>
      </div>
      <el-table
        :key="liftTableKey"
        :data="liftTableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        style="width: 100%;">

        <el-table-column label="码头作业代码" property="actType"/>
        <el-table-column label="码头作业名称" property="name"/>
        <el-table-column label="20尺所需费用" property="twentyCost"/>
        <el-table-column label="40尺所需费用" property="fortyCost"/>
      </el-table>
    </el-card>

    <el-card shadow="never" class="app-container margin-content no-padding">
      <div slot="header" class="clearfix">
        <div class="header fl">移位费用明细（包括短距离运输、拆箱、装箱费用）（单位：元）</div>
      </div>

      <el-table
        :key="moveTableKey"
        :data="moveTableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        style="width: 100%;">

        <el-table-column label="码头作业代码" property="actType"/>
        <el-table-column label="码头作业名称" property="name"/>
        <el-table-column label="20尺所需费用" property="twentyCost"/>
        <el-table-column label="40尺所需费用" property="fortyCost"/>
      </el-table>
    </el-card>

    <el-card shadow="never" class="app-container margin-content no-padding">
      <div slot="header" class="clearfix">
        <div class="header fl">仓储费用明细（单位：元/天）</div>
      </div>

      <el-table
        :key="storageTableKey"
        :data="storageTableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        style="width: 100%;">

        <el-table-column label="码头作业代码" property="actType"/>
        <el-table-column label="码头作业名称" property="name"/>
        <el-table-column label="20尺所需费用" property="twentyCost"/>
        <el-table-column label="40尺所需费用" property="fortyCost"/>
      </el-table>
    </el-card>

    <div class="toolbar clearfix">
      <div class="fl"></div>
      <div class="fr">
        <el-button type="primary" icon="el-icon-news" @click="handleAudit(1)">确认</el-button>
        <el-button type="danger" icon="el-icon-refresh" @click="handleAudit(2)">退回</el-button>
      </div>
    </div>

    <!-- 弹窗, 审核 -->
    <cost-audit v-if="auditVisible" ref="audit" @refresh="_getAll()"></cost-audit>
  </div>
</template>

<script>
  // api
  import { getAll } from '@/api/costSubsidies'
  // vuex
  import { mapGetters } from 'vuex'
  // components
  import CostAudit from './components/cost-audit'

  const MAP = {
    0: {
      text: '未审核',
      type: 'warning'
    },
    1: {
      text: '已审核',
      type: 'success'
    },
    2: {
      text: '退回',
      type: 'error'
    }
  }

  export default {
    name: 'cost-manage-detail',
    data() {
      return {
        liftTableKey: 0,
        moveTableKey: 1,
        storageTableKey: 2,
        tableData: [],
        loading: false,

        auditStatus: 0,
        auditVisible: false
      }
    },
    computed: {
      liftTableData() {
        return this.tableData.filter((data) => {
          return data.costType === 1
        })
      },
      moveTableData() {
        return this.tableData.filter((data) => {
          return data.costType === 2
        })
      },
      storageTableData() {
        return this.tableData.filter((data) => {
          return data.costType === 3
        })
      },
      currentAuditStatus() {
        return MAP[this.auditStatus]
      },
      ...mapGetters([
        'currentId'
      ])
    },
    created() {
      this._getAll()
    },
    methods: {
      _getAll() {
        this.loading = true
        const { portId } = this.currentId
        getAll({ portId }).then((res) => {
          this.tableData = res.data.data
          if (this.tableData.length > 0) {
            this.auditStatus = this.tableData[0].auditStatus
          }
          this.loading = false
        })
      },
      handleAudit(auditStatus) {
        this.auditVisible = true
        this.$nextTick(() => {
          this.$refs['audit'].init(this.currentId, auditStatus)
        })
      },
      handleReturn(auditStatus) {
        this.auditVisible = true
        this.$nextTick(() => {
          this.$refs['audit'].init(this.currentId, auditStatus)
        })
      },
      back() {
        this.$parent.back()
      }
    },
    components: {
      CostAudit
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss" scoped>
  .el-card__header {
    padding: 10px;
    .header {
      padding: 2px 0;
    }
    .extra {
      padding: 0 !important;
    }
    .el-alert {
      padding: 2px 16px;
    }
  }
</style>
